import addNsname from '../add-nsname'
import { constantRoutes, asyncRoutes } from '@/router'
const name = 'permission' // 模块名
const nsname = name + '/' // 命名空间模块名

const state = {
  routes: constantRoutes
}

const mutations = {
  addRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
addNsname(nsname, mutations)

const actions = {
  asyncAddRoutes(ctx, menus) {
    const routes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
    ctx.commit(mutations.addRoutes.nsname, routes, { root: true })
    return routes
  }
}
addNsname(nsname, actions)

export default {
  name,
  namespaced: true,
  state,
  mutations,
  actions
}
